﻿using System;

using SharedGenomics.Core;

namespace SharedGenomics.Domain
{
    /// <summary>
    /// Default implementation of <see cref="IAnalysisResultsFileReadStrategy"/> which allows all lines to be read and keeps track of the number of lines
    /// read so far. Subtypes can provide more complex read strategies by overriding the ShouldAddAndContinueCore method.
    /// </summary>
    public class DefaultAnalysisResultsFileReadStrategy : IAnalysisResultsFileReadStrategy
    {
        public void Reset()
        {
            this.LinesRead = 0;
            this.ResetCore();
        }

        public bool ShouldAddAndContinue(AnalysisResult result)
        {
            result.ThrowIfNull("result");

            bool shouldContinue = this.ShouldAddAndContinueCore(result);
            if (shouldContinue)
            {
                this.LinesRead++;
            }

            return shouldContinue;
        }

        /// <summary>
        /// Implements the logic associated with the ShouldAddAndContinue method - if this returns false the parse should stop and the current item should not 
        /// be used (i.e. the current item is the first item to fail the cutoff)
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        protected virtual bool ShouldAddAndContinueCore(AnalysisResult result)
        {
            return true;
        }

        protected virtual void ResetCore()
        {
        }

        /// <summary>
        /// The number of lines read so far
        /// </summary>
        public int LinesRead { get; private set; }
    }
}
